#!/usr/bin/env bash
# 抖音主播数据指标统计任务脚本

# 需要吧这行内容添加到 crontab 中， crontab -e
# 每天凌晨 00:30 执行一次
# 30 00 * * * /bin/sh /home/hadoop/jobs/tiktok/video_info_task.sh >> /home/hadoop/jobs/tiktok/video_info_task.log

# 防止shell脚本手动执行没问题，crontab定时执行失败
source /etc/profile
source ~/.bash_profile

# 判断用户是否输入日期参数，如果没有输入则默认获取昨天日期
if [ "X$1" = "X" ]; then
    yes_time=`date +%Y%m%d --date="1 days ago"`
else
    yes_time=$1
fi

cleanjob_input=hdfs://hadoop01:9000/data/tiktok/anchor/videoinfo/${yes_time}
cleanjob_output=hdfs://hadoop01:9000/data/tiktok/anchor/videoinfo_clean/${yes_time}

videoinfojob_input=${cleanjob_output}
videoinfojob_output=hdfs://hadoop01:9000/res/tiktok/anchor/videoinfojob/${yes_time}

videoinfojobtop10_input=${cleanjob_output}
videoinfojobtop10_output=hdfs://hadoop01:9000/res/tiktok/anchor/videoinfojobtop10/${yes_time}

# 工作目录
jobs_home=/home/hadoop/jobs/tiktok
jar_name=bigdata-0.0.1-SNAPSHOT-jar-with-dependencies.jar

# 删除输出目录 为了兼容脚本重跑的情况
hdfs dfs -rm -r ${cleanjob_output}
hdfs dfs -rm -r ${videoinfojob_output}
hdfs dfs -rm -r ${videoinfojobtop10_output}

# 执行数据清洗任务
hadoop jar \
${jobs_home}/${jar_name} \
com.bkhech.bigdata.tiktok_anchor.dataclean.DataCleanJob \
${cleanjob_input} \
${cleanjob_output}
# 判断数据清洗任务是否执行成功
hdfs dfs -ls ${cleanjob_output}/_SUCCESS
if [ "$?" = "0" ]; then
    echo "DataCleanJob execute success... date time is ${yes_time}"
     # 执行指标统计任务1
      hadoop jar \
      ${jobs_home}/${jar_name} \
      com.bkhech.bigdata.tiktok_anchor.videoinfo.VideoInfoJob \
      ${videoinfojob_input} \
      ${videoinfojob_output}
      hdfs dfs -ls ${videoinfojob_output}/_SUCCESS
      if [ "$?" != "0" ]; then
          echo "VideoInfoJob execute failed..."
          # 此处可以给管理员发短信、发邮件，可以调用接口或者脚本发送
      fi

      # 执行指标统计任务2
      hadoop jar \
            ${jobs_home}/${jar_name} \
            com.bkhech.bigdata.tiktok_anchor.videoinfotop10.VideoInfoJobTop10 \
            ${videoinfojobtop10_input} \
            ${videoinfojobtop10_output} \
            ${yes_time}
      hdfs dfs -ls ${videoinfojobtop10_output}/_SUCCESS
      if [ "$?" != "0" ]; then
          echo "VideoInfoJobTop10 execute failed..."
          # 此处可以给管理员发短信、发邮件，可以调用接口或者脚本发送
      fi
else
  echo "DataCleanJob execute failed... date time is ${yes_time}"
  # 此处可以给管理员发短信、发邮件，可以调用接口或者脚本发送
fi